package com.example.admin.wine;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton btnHome;
    private ImageButton btnType;
    private ImageButton btnCart;
    private ImageButton btnMy;

    private HomeFragment homeFragment = null;
    private TypeFragment typeFragment = null;
    private CartFragment cartFragment = null;
    private MyFragment myFragment = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnHome = findViewById(R.id.btn_home);
        btnType = findViewById(R.id.btn_type);
        btnCart = findViewById(R.id.btn_cart);
        btnMy = findViewById(R.id.btn_my);

        btnHome.setOnClickListener(this);
        btnType.setOnClickListener(this);
        btnCart.setOnClickListener(this);
        btnMy.setOnClickListener(this);

        selectTab(0);
    }
    @Override
    public void onClick(View v) {
        setdefault();
        switch (v.getId()){
            case R.id.btn_home:
                btnHome.setImageResource(R.drawable.home_selected);
                selectTab(0);
                break;
            case R.id.btn_type:
                btnType.setImageResource(R.drawable.type_selected);
                selectTab(1);
                break;
            case R.id.btn_cart:
                btnCart.setImageResource(R.drawable.cart_selected);
                selectTab(2);
                break;
            case R.id.btn_my:
                btnMy.setImageResource(R.drawable.my_selected);
                selectTab(3);
                break;
        }

    }

    public void selectTab(int index){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if(homeFragment != null){
            transaction.hide(homeFragment);
        }
        if(typeFragment != null){
            transaction.hide(typeFragment);
        }
        if(cartFragment != null){
            transaction.hide(cartFragment);
        }
        if(myFragment != null){
            transaction.hide(myFragment);
        }
        switch (index){
            case 0:
                if(homeFragment == null){
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.content,homeFragment);
                }else {
                    transaction.show(homeFragment);
                }break;
            case 1:
                if(typeFragment == null){
                    typeFragment = new TypeFragment();
                    transaction.add(R.id.content,typeFragment);
                }else {
                    transaction.show(typeFragment);
                }break;
            case 2:
                if(cartFragment == null){
                    cartFragment = new CartFragment();
                    transaction.add(R.id.content,cartFragment);
                }else {
                    transaction.show(cartFragment);
                }break;
            case 3:
                if(myFragment == null){
                    myFragment = new MyFragment();
                    transaction.add(R.id.content,myFragment);
                }else {
                    transaction.show(myFragment);
                }break;
        }transaction.commit();
    }

    public void setdefault(){
        btnHome.setImageResource(R.drawable.home_default);
        btnType.setImageResource(R.drawable.type_default);
        btnCart.setImageResource(R.drawable.cart_default);
        btnMy.setImageResource(R.drawable.my_default);
    }

}
